package com.dios.mirage.component.fields;

import java.util.Vector;

import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.TouchEvent;

public class SelectField extends FocusableField {

	public Vector listImage;
	public int imageAt = 0;
	public int imageSize;
	private int _xTouch = 0;
	
	public SelectField(Vector imageList) {
		super(null, Field.FOCUSABLE);
		this.listImage = imageList;
		this.imageSize = imageList.size();
		setImage((EncodedImage) imageList.elementAt(0));
	}

	protected boolean touchEvent(TouchEvent touchEvent) {
		int event = touchEvent.getEvent();
		if (event == TouchEvent.DOWN && touchEvent.isValid()) {
			_xTouch  = touchEvent.getX(1);
		} else if (event == TouchEvent.MOVE && touchEvent.isValid()) {
			if (_xTouch < touchEvent.getX(1)) {
				imageAt--;
				if (imageAt == -1)
					imageAt = imageSize - 1;
				this.setImage((EncodedImage) listImage
						.elementAt(imageAt));
			} else if (_xTouch > touchEvent.getX(1)) {
				imageAt++;
				if (imageAt == imageSize)
					imageAt = 0;
				this.setImage((EncodedImage) listImage
						.elementAt(imageAt));
			}
			_xTouch = touchEvent.getX(1);
		}
		return true;
	}

	protected boolean navigationMovement(int dx, int dy, int status, int time) {
		if (dx > 0) {
			imageAt--;
			if (imageAt == -1)
				imageAt = imageSize - 1;
			this.setImage((EncodedImage) listImage.elementAt(imageAt));
		} else if (dx < 0) {
			imageAt++;
			if (imageAt == imageSize)
				imageAt = 0;
			this.setImage((EncodedImage) listImage.elementAt(imageAt));
		}
		return super.navigationMovement(dx, dy, status, time);
	}
}

